//
//  LoginViewController.m
//  YellowBridge
//
//  Created by 王右 on 2018/12/1.
//  Copyright © 2018 王右. All rights reserved.
//

#import "LoginViewController.h"
#import "HomePageViewController.h"
#import "LoginUserModel.h"
#import "ResetPasswordViewController.h"
@interface LoginViewController ()

@property (weak, nonatomic) IBOutlet UITextField *userNameTextField;

@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;

@property (weak, nonatomic) IBOutlet UIButton *loginButton;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.fd_prefersNavigationBarHidden = YES;
    [self configAllSubViews];
    _userNameTextField.text = [[NSUserDefaults standardUserDefaults] valueForKey:@"LoginUserAccountCache"];
}

- (void)configAllSubViews{
    _loginButton.layer.cornerRadius = 22.5f;
    _loginButton.layer.masksToBounds = YES;
}
- (IBAction)loginAction:(UIButton *)sender {
    if (_userNameTextField.text.length ==0) {
        [self.view makeToast:@"账号不能为空!"];
        return;
    }
    if (_passwordTextField.text.length == 0) {
        [self.view makeToast:@"密码不能为空!"];
        return;
    }
    [self hudWithText:@"登录中..."];
//    @"account":@"13402627262",@"password":@"12345678"
    [RequestManager postRequestWithModule:@"v1/login" param:@{@"account":_userNameTextField.text,@"password":_passwordTextField.text} model:nil success:^(NSDictionary *obj) {
        [self removeHud];
        NSDictionary *user = obj[@"result"];
        if (![user[@"check_result"] boolValue]) {
            [self.view makeToast:obj[@"message"]];
            return ;
        }
        LoginUserModel *model = [LoginUserModel objectWithDictionary:user];
//        model.account = @"13402627262";
        model.account = self->_userNameTextField.text;
        model.password = self->_passwordTextField.text;
        [[NSUserDefaults standardUserDefaults] setValue:self->_userNameTextField.text forKey:@"LoginUserAccountCache"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [[CCCache defaultManager] setObject:model forKey:Login_User_Info_Model];
        [UIApplication sharedApplication].keyWindow.rootViewController = [[BaseNavigationViewController alloc] initWithRootViewController:[HomePageViewController new]];
    } failure:^(NSString *error) {
        [self removeHud];
        [self.view makeToast:error];
    }];
}

- (IBAction)forgetPasswordAction:(id)sender {
    [self.navigationController pushViewController:[ResetPasswordViewController new] animated:YES];
}

@end
